<?php
/**
 * @title 497. 非重叠矩形中的随机点 - 二分
 * @author start2004
 */

// ini_set("memory_limit", "128M");

// class Solution {
//
//     function find() {
//
//     }
// }

class Solution {
    /**
     * @param Integer[][] $rects
     */
    function __construct($rects) {
        $max = 0;
        $this->numArray = [];

        /**
         * @since 2022-06-10 记录点
         */
        foreach ($rects as list($x1, $y1, $x2, $y2)){
            $max = $max + ($x2-$x1+1) * ($y2-$y1+1);
            // echo "max = {$max}", PHP_EOL;
            $this->numArray[] = [$max, $x1, $y1, $x2, $y2];
        }
    }

    /**
     * @return Integer[]
     */
    function pick() {
        /**
         * @since 2022-06-10 长度
         */
        $len = count($this->numArray);

        /**
         * @since 2022-06-10 随机数
         */
        $rand = rand(1, $this->numArray[$len-1][0]);
        // echo "rand = {$rand}", PHP_EOL;

        /**
         * @since 2022-06-10 二分查找
         */
        $s = 0;
        $e = $len-1;
        while($s < $e){
            $mid = ($s+$e)>>1;
            $val = $this->numArray[$mid][0];

            if($rand <= $val){
                $e = $mid;
            } else {
                $s = $mid+1;
            }
        }

        /**
         * @since 2022-06-10 获取矩形
         */
        list($num, $x1, $y1, $x2, $y2) = $this->numArray[$e];
        // echo "key={$e}, ({$x1}, {$y1}, {$x2}, {$y2}), num = {$num}", PHP_EOL;

        /**
         * @since 2022-06-10 宽，高
         */
        $w = $x2-$x1+1;
        $h = $y2-$y1+1;

        /**
         * @since 2022-06-10 距离
         */
        $d = $num - $rand;

        /**
         * @since 2022-06-10 高度和宽度偏差
         */
        $addY = intval($d/$w);
        $addX = $d % $w;
        // echo "d = {$d}, x+{$addX}, y+{$addY}", PHP_EOL;

        /**
         * @return
         */
        return [$x1+$addX, $y1+$addY];
    }
}

/**
 * @url http://127.0.0.1/leetcode/202206/2022.06.09.php
 */
$datas = [
    // [
    //     ["Solution","pick","pick","pick","pick","pick"],
    //     [[[[0,0,1,1]]],[],[],[],[],[]],
    // ],
    [
        ["Solution","pick","pick","pick","pick","pick"],
        [[[[-2,-2,1,1],[2,2,4,6]]],[],[],[],[],[]],
    ],
];

include_once dirname(__DIR__) . DIRECTORY_SEPARATOR ."xhprof.php";
$xhprof = new Xhprof();
// foreach ($datas as $data){
//     if(count($data) <= 20){
//         var_dump($data);
//     } else {
//         var_dump(count($data));
//     }
//
//     $obj = new Solution();
//     $result = $obj->(...$data);
//     // $result = $obj->($xhprof->tree($data));
//     // $result = $obj->($xhprof->listNode($data));
//     // $result = $obj->find(...$data);
//     var_dump($result);
//     // if(count($result)<=20){
//     //     var_dump($result);
//     // } else {
//     //     var_dump(count($result));
//     // }
//     echo str_repeat(PHP_EOL, 3);
//     echo str_repeat("<br>", 3);
// }

foreach ($datas as $data){
    $obj = new $data[0][0](...$data[1][0]);

    for ($i=1; $i<count($data[0]); $i++){
        $func = $data[0][$i];
        $param = $data[1][$i];
        $result = $obj->$func(...$param);

        echo $data[0][$i] ."(\"". implode(",", $data[1][$i]) ."\") ";
            if($result === true){
                echo "True";
            } elseif($result === false) {
                echo "False";
            } elseif($result === null) {
                echo "Null";
            } elseif(is_array($result)) {
                var_dump($result);
            } else {
                echo $result;
            }
        echo PHP_EOL;
    }

    echo str_repeat(PHP_EOL, 3);
}
$xhprof->end();
